package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatTwoResponse;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/10/12.
 */

public class TwoSidedStatisticsAdapter extends BaseAdapter {
    private Context mContext;
    private LotteryStatTwoResponse lotteryStatTwoResponses;
    public TwoSidedStatisticsAdapter(Context context, LotteryStatTwoResponse lotteryStatTwoResponse){
        mContext = context;
        lotteryStatTwoResponses = lotteryStatTwoResponse;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.prize_number_item1, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.title.setText(lotteryStatTwoResponses.getBall().get(i).get(0));
        if(i == 0){
            holder.title.setBackgroundResource(R.drawable.bg_corners_100_7);
        } else if(i == 1){
            holder.title.setBackgroundResource(R.drawable.bg_corners_100_9);
        } else {
            holder.title.setBackgroundResource(R.drawable.bg_corners_100_10);
        }
        String[] keys = lotteryStatTwoResponses.getBall().get(i).get(1).split(",");
        String[] values = lotteryStatTwoResponses.getBall().get(i).get(2).split(",");
        holder.title1.removeAllViews();
        holder.title2.removeAllViews();
        if(keys.length == values.length){
            for(int p = 0; p < keys.length; p++){
                TextView textView = new TextView(mContext);
                textView.setText(keys[p]);
                textView.setTextColor(Color.parseColor("#3f3f3f"));
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                textView.setGravity(Gravity.CENTER);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.weight = 1;
                layoutParams.gravity = Gravity.CENTER;
                textView.setLayoutParams(layoutParams);
                holder.title1.addView(textView);

                TextView textView2 = new TextView(mContext);
                textView2.setText(values[p]);
                textView2.setTextColor(Color.parseColor("#3f3f3f"));
                textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams2.weight = 1;
                layoutParams2.gravity = Gravity.CENTER;
                textView2.setGravity(Gravity.CENTER);
                textView2.setLayoutParams(layoutParams2);
                holder.title2.addView(textView2);
            }
        }

        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return lotteryStatTwoResponses.getBall().size();
    }

    static class ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.title1)
        LinearLayout title1;
        @BindView(R.id.title2)
        LinearLayout title2;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
